class AlunosController < ApplicationController
  # GET /alunos
  # GET /alunos.xml
  def index
    @alunos = Aluno.find(:all, :order => 'nome asc')

    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @alunos }
    end
  end

  # GET /alunos/1
  # GET /alunos/1.xml
  def show
    @aluno = Aluno.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @aluno }
    end
  end

  # GET /alunos/new
  # GET /alunos/new.xml
  def new
    get_escola
    get_estados
    @aluno = Aluno.new
    @aluno.status = 1
    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @aluno }
    end
  end

  # GET /alunos/1/edit
  def edit
    get_escola
    get_estados
    @aluno = Aluno.find(params[:id])
  end

  # POST /alunos
  # POST /alunos.xml
  def create
    get_escola
    get_estados
    @aluno = Aluno.new(params[:aluno])

    respond_to do |format|
      if @aluno.save
        flash[:notice] = 'Aluno was successfully created.'
        format.html { redirect_to(@aluno) }
        format.xml  { render :xml => @aluno, :status => :created, :location => @aluno }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @aluno.errors, :status => :unprocessable_entity }
      end
    end
  end

  # PUT /alunos/1
  # PUT /alunos/1.xml
  def update
    get_escola
    get_estados
    @aluno = Aluno.find(params[:id])

    respond_to do |format|
      if @aluno.update_attributes(params[:aluno])
        flash[:notice] = 'Aluno was successfully updated.'
        format.html { redirect_to(@aluno) }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @aluno.errors, :status => :unprocessable_entity }
      end
    end
  end

  # DELETE /alunos/1
  # DELETE /alunos/1.xml
  def destroy
    @aluno = Aluno.find(params[:id])
    @aluno.destroy

    respond_to do |format|
      format.html { redirect_to(alunos_url) }
      format.xml  { head :ok }
    end
  end

  def search
        @alunos = Aluno.find(:all, :conditions => ['nome like ?', params[:nome] + "%"])
        render :text => @alunos.to_json
  end
  
  def historico
    @aluno = Aluno.find(params[:id])
    @aulas = []
    aula_alunos = AulaAlunos.find(:all, :conditions => {:aluno_id => @aluno.id })
    aula_alunos.each do |aula_aluno|
        @aulas << aula_aluno.aula
    end
    
  end

  private 
    def get_escola
        @escolas = Escola.find(:all).collect {|e| [e.nome, e.id]}
    end



end
